#!/bin/bash
########################
####
####    Installation script for JavaScript Unit Testing Suite
####    Author: Alberto Navarro / Tyler Beck
####    Version: 0.0.24
####
########################

export VERSION='0.0.24';
export ESERVER=/opt/intershop/; # where our eserver's reside
export NODE_VERSION=v0.10.22; # which node version we will be installing

export ESERVERCOUNT=0; # number of eservers
export WEBSTORECOUNT=0; # number of webstores

export REFUSER='v11refstoreteam'
export REFPASS='v11refstoreteam'

clear # Clear our terminal screen 

echo "########################################################"
echo "###"
echo "$(tput setaf 6)###   Installing JUTS v$VERSION"
echo "###   Company: eBay Enterprise"
echo "###   Author: Tyler Beck"
echo "$(tput sgr0)###"
echo "########################################################"
echo "###"
echo "$(tput setaf 6)###   Please select which eserver you want to install JUTS on"
echo "###   Instructions: Please enter the corresponding number and then press 'enter'."
echo "$(tput sgr0)###"
echo "########################################################"
echo ""

for fd in ${ESERVER}*; do
    
    if [ -d "$fd" ]; then

        echo "$[ESERVERCOUNT+1].) $fd"
        
        ESERVERCOUNT=$[ESERVERCOUNT+1];

    fi
    
done

echo ""

while true; do
  
  read -p "Please choose an (e)server: " server_input
  
  case $server_input in
  
      ''|*[!0-9]*) echo "Incorrect data, please use numeric values only."; ;;
      *) break;;
      
  esac

done

BRANCH=$(svn list  http://devsvn.gspt.net/svn/components/sandbox/js/webstores/ --no-auth-cache --non-interactive --username=$REFUSER --password=$REFPASS)
REFARRAY=(${BRANCH/\r\n/ })

clear

echo "########################################################"
echo "###"
echo "$(tput setaf 6)###   Please select which webstore you are running on eserver$server_input"
echo "###   Instructions: Please enter the corresponding number and then press 'enter'."
echo "$(tput sgr0)###"
echo "########################################################"
echo ""

for item in "${REFARRAY[@]}"; do

  echo "$[WEBSTORECOUNT+1].) $item"
        
  WEBSTORECOUNT=$[WEBSTORECOUNT+1];
    
done

echo ""

while true; do
  
  read -p "Please choose the webstore your using: " webstore_input
  
  case $webstore_input in
  
      ''|*[!0-9]*) echo "Incorrect data, please use numeric values only."; ;;
      *) break;;
      
  esac

done

clear
echo "########################################################"
echo "###"
echo "$(tput setaf 6)###   JUTS: Downloading Dependencies..."
echo "$(tput sgr0)###"
echo "########################################################"
sleep 2

if ! hash "nvm" >/dev/null 2>&1; then
    echo "Installing NVM %s\n" >&2
    curl https://raw.github.com/creationix/nvm/master/install-gitless.sh | sh;
fi
wait

source $HOME/.bash_profile;
cd $HOME;
nvm install $NODE_VERSION;

wait
echo "########################################################"
echo "###"
echo "$(tput setaf 6)###   JUTS: Node Installation Complete..."
echo "$(tput sgr0)###"
echo "########################################################"

source $HOME/.bash_profile;
npm install -g phantomjs;

wait
echo "########################################################"
echo "###"
echo "$(tput setaf 6)###   JUTS: PhantomJS Installation Complete..."
echo "$(tput sgr0)###"
echo "########################################################"
echo ""

commands_tocheck="nvm phantomjs"
missing_counter=0;

for needed_command in $commands_tocheck; do
  if ! hash "$needed_command" >/dev/null 2>&1; then
    echo "Command not found in PATH: %s\n" "$needed_command" >&2
    ((missing_counter++))
  fi
done

if ((missing_counter > 0)); then
  echo "There was something wrong with the installation. Please contact the Reference Store Team" >&2
  exit 1
fi

if [ -d "$HOME/.nvm/$NODE_VERSION/bin" ]; then

  echo "PATH=\$PATH:\$HOME/.nvm/$NODE_VERSION/bin" >> "$HOME/.bashrc";
  echo "alias jutstest$server_input=\"clear && node /development.$server_input/v11ext/server/server.js\"" >> "$HOME/.bashrc";
  source $HOME/.bashrc

  sudo node_path="$HOME/.nvm/$NODE_VERSION/bin" sh -c 'echo "PATH=\$PATH:$node_path" >> "$HOME/.bashrc"';
  sudo server_input="$server_input" sh -c 'echo "alias juts_server$server_input=\"clear && node /development.$server_input/v11ext/server/server.js -w\"" >> "$HOME/.bashrc"';
  sudo sh -c 'source $HOME/.bashrc'

fi

cd /development.$server_input/v11ext/;
svn co "http://devsvn.gspt.net/svn/components/sandbox/js/server" --no-auth-cache --non-interactive --username=$REFUSER --password=$REFPASS
wait

cd /development.$server_input/v11ext/source/;
svn co "http://devsvn.gspt.net/svn/components/sandbox/js/webstores/${REFARRAY[$[webstore_input-1]]}gsi_pf_juts" --no-auth-cache --non-interactive --username=$REFUSER --password=$REFPASS
wait

clear

if [[ -d "/development.$server_input/v11ext/server" && -d "/development.$server_input/v11ext/source/gsi_pf_juts" ]]; then

    echo "########################################################"
    echo "###"
    echo "$(tput setaf 6)###   JUTS: Setting up shortcuts..."
    echo "$(tput sgr0)###"
    echo "########################################################"
    echo ""

    sudo server_input="$server_input" sh -c 'echo "
      <Menu>
        <Name>JUTS</Name>
        <NotDeleted/>
        <Menu>
          <Name>JUTS</Name>
          <Directory>juts.directory</Directory>
          <NotDeleted/>
          <Menu>
            <Name>eserver$server_input</Name>
            <Directory>juts-eserver$server_input.directory</Directory>
            <NotDeleted/>
            <Include>
              <Filename>juts-regular$server_input.desktop</Filename>
              <Filename>juts-open$server_input.desktop</Filename>
              <Filename>juts-regular-all$server_input.desktop</Filename>
              <Filename>juts-open-all$server_input.desktop</Filename>
            </Include>
          </Menu>
        </Menu>
      </Menu>
    " > /etc/xdg/menus/applications-merged/juts.menu'

    if [ ! -f "/usr/share/desktop-directories/juts.directory" ]; then

      sudo server_input="$server_input" sh -c 'echo "
        [Desktop Entry]
        Icon=/development.$server_input/v11ext/server/lib/scripts/icons/juts-icon.png
        Type=Directory
        Encoding=UTF-8
        Name=JUTS
      " > /usr/share/desktop-directories/juts.directory'
    
    fi

    sudo server_input="$server_input" sh -c 'echo "
      [Desktop Entry]
      Icon=intershop.png
      Type=Directory
      Encoding=UTF-8
      Name=eserver$server_input
    " > "/usr/share/desktop-directories/juts-eserver$server_input.directory"'

    sudo server_input="$server_input" sh -c 'echo "
      [Desktop Entry]
      Type=Application
      Terminal=true
      Name=Run JUTS - Use Terminal Results
      Icon=/development.$server_input/v11ext/server/lib/scripts/icons/juts-icon.png
      Exec=bash /development.$server_input/v11ext/server/lib/scripts/launch.sh /development.$server_input/v11ext/server
    " > "/usr/share/applications/juts-regular$server_input.desktop"'

    sudo server_input="$server_input" sh -c 'echo "
      [Desktop Entry]
      Type=Application
      Terminal=true
      Name=Run JUTS - Use Firefox Results
      Icon=/development.$server_input/v11ext/server/lib/scripts/icons/juts-icon.png
      Exec=bash /development.$server_input/v11ext/server/lib/scripts/launch.sh /development.$server_input/v11ext/server true
    " > "/usr/share/applications/juts-open$server_input.desktop"'

    sudo server_input="$server_input" sh -c 'echo "
      [Desktop Entry]
      Type=Application
      Terminal=true
      Name=Run JUTS - Use Firefox Results (Show All)
      Icon=/development.$server_input/v11ext/server/lib/scripts/icons/juts-icon.png
      Exec=bash /development.$server_input/v11ext/server/lib/scripts/launch.sh /development.$server_input/v11ext/server true true
    " > "/usr/share/applications/juts-regular-all$server_input.desktop"'

    sudo server_input="$server_input" sh -c 'echo "
      [Desktop Entry]
      Type=Application
      Terminal=true
      Name=Run JUTS - Use Terminal Results (Show All)
      Icon=/development.$server_input/v11ext/server/lib/scripts/icons/juts-icon.png
      Exec=bash /development.$server_input/v11ext/server/lib/scripts/launch.sh /development.$server_input/v11ext/server false true
    " > "/usr/share/applications/juts-open-all$server_input.desktop"'

    sleep 1

    echo "########################################################"
    echo "###"
    echo "$(tput setaf 6)###   JUTS: Finished Installation!"
    echo "$(tput sgr0)###"
    echo "########################################################"
    echo ""

    notify-send -t 20000 -u low -i "important" "JUTS is now installed on eserver$server_input; you can access JUTS from your Applications Menu."

  else

    exit 0

fi